package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.dkd.common.constant.DkdContants.VM_STATUS_NODEPLOY;

/**
 * 设备管理Service业务层处理
 * 
 * @author weihzicong
 * @date 2024-10-22
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Resource
    private VendingMachineMapper vendingMachineMapper;

    @Resource
    private VmTypeMapper vmTypeMapper;

    @Resource
    private NodeMapper nodeMapper;

    @Resource
    private IChannelService channelService;
    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertVendingMachine(VendingMachine vendingMachine)
    {

        // 1. 新增设备
        // 1-1: 8位编号ID
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        // 1-2: 查询售货机类型表，补充设备容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        // 1-3: 查询点位信息、补充区域、合作商信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine,"id");
        // 手动添加addr
        vendingMachine.setAddr(node.getAddress());
        // 1-4: 设备状态
        vendingMachine.setVmStatus(VM_STATUS_NODEPLOY);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        // 1-5: 保存
        int result =  vendingMachineMapper.insertVendingMachine(vendingMachine);

        // 2: 新增货道信息
        List<Channel> channelList = new ArrayList<Channel>();
        for(int i=1; i<=vmType.getVmRow(); i++) {
            for(int j=1; j<=vmType.getVmCol(); j++) {
                // 封装channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j); // 货道编号
                channel.setVmId(vendingMachine.getId()); // 售货机ID
                channel.setInnerCode(vendingMachine.getInnerCode()); // 货道内部编码
                channel.setMaxCapacity(vmType.getChannelMaxCapacity()); // 货道最大容量
                channel.setCreateTime(vendingMachine.getCreateTime()); // 创建时间
                channel.setUpdateTime(vendingMachine.getUpdateTime()); // 更新时间

                // 保存到集合中
                channelList.add(channel);
            }
        }
        // 统一批量保存货道
        channelService.batchInsertChannels(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if(vendingMachine.getNodeId() != null) { // 有点位说明在更新设备
            // 查询点位表，补充区域、点位、合作商信息
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node, vendingMachine,"id");
            // 手动添加addr
            vendingMachine.setAddr(node.getAddress());  // 更新地址
        }
        // 否则，说明是更新设备的策略信息
        vendingMachine.setUpdateTime(DateUtils.getNowDate()); // 更新时间
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }


    /**
     * 根据设备编号查询设备信息
     * @param innerCode
     * @return
     */
    @Override
    public VendingMachine selectMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectMachineByInnerCode(innerCode);
    }
}
